<template>
  <div class="app-container">
    <h1>资源领用</h1>
    <el-table :data="availableResources" style="width: 100%">
      <el-table-column prop="name" label="资源名称"></el-table-column>
      <el-table-column prop="quantity" label="可用数量"></el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button size="small" @click="handleClaim(scope.$index, scope.row)">领用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="领用资源" :visible.sync="dialogVisible" width="30%">
      <el-form :model="claimForm" label-width="100px">
        <el-form-item label="资源名称">
          <el-input v-model="claimForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="领用量">
          <el-input-number v-model="claimForm.claimedQuantity" :min="1"
            :max="claimForm.availableQuantity"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmClaim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      availableResources: [
        { id: 1, name: '农药A', availableQuantity: 10 },
        { id: 2, name: '喷雾器', availableQuantity: 5 },
        { id: 3, name: '除草机', availableQuantity: 3 }
      ],
      dialogVisible: false,
      claimForm: {
        id: null,
        name: '',
        claimedQuantity: 0,
        availableQuantity: 0
      }
    };
  },
  methods: {
    handleClaim(index, row) {
      this.claimForm.id = row.id;
      this.claimForm.name = row.name;
      this.claimForm.claimedQuantity = 0;
      this.claimForm.availableQuantity = row.availableQuantity;
      this.dialogVisible = true;
    },
    confirmClaim() {
      const resourceIndex = this.availableResources.findIndex(resource => resource.id === this.claimForm.id);
      if (resourceIndex !== -1) {
        this.availableResources[resourceIndex].availableQuantity -= this.claimForm.claimedQuantity;
        // 这里可以添加实际的领用逻辑，比如记录领用人等
      }
      this.dialogVisible = false;
      console.log('资源已领用:', this.claimForm);
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>



